package com.hnkywl.jip.dao;

import com.hnkywl.jip.utils.SubjectData;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.jip.dao
 * 类描述:    [一句话描述该类的功能]
 * 创建人:    ZHUCHG
 * 创建时间: 13-3-2 下午4:32
 * 修改人:    ZHUCHG
 * 修改时间: 13-3-2 下午4:32
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class SubjectDao extends SimpleHibernateTemplate {

    public List findCommonSubject(Integer companyId) {
        String sql = "select s1.code_id,c.compid,comapny_name=dbo.gf_compid_fullname(c.compid),s1.remarks,s1.subject_name,s2.code,s2.dept_id " +
                " from t_jiuqi_common_subject s1 cross join t_sys_comp c left join t_jiuqi_common_subject_code s2 " +
                " on s2.code_id=s1.code_id and c.compid=s2.company_id where c.compid=? order by s1.code_id";
        List<Object[]> result = createSQLQuery(sql, companyId).list();
        if (result != null && !result.isEmpty())
            return result;
        return null;
    }

    public void saveSubjectCode(List<SubjectData> data) {
        String sql = "insert into t_jiuqi_common_subject_code(code_id,company_id,code,dept_id) values(?,?,?,?)";
        int t = 0;
        for (SubjectData subjectData : data) {
            createSQLQuery(sql, subjectData.getCodeId(), subjectData.getCompanyId(), subjectData.getCode(), subjectData.getDeptId()).executeUpdate();
            if (t % 30 == 0)
                this.getSession().flush();
            t++;
        }
    }

    public void deleteSubjectCode(List<SubjectData> data) {
        String sql = "delete from t_jiuqi_common_subject_code where code_id=? and company_id=?";
        int t = 0;
        for (SubjectData subjectData : data) {
            createSQLQuery(sql, subjectData.getCodeId(), subjectData.getCompanyId()).executeUpdate();
            if (t % 30 == 0)
                this.getSession().flush();
            t++;
        }
    }

}
